﻿// 1.2.7. Write a program, that reads from the console an array of N integers and an integer K,
// sorts the array and using the method Array.BinSearch() finds the largest number in the array which is ≤ K. 


using System;

class UsingBinSearch
{
    static void Main()
    {
        int[] arr = { 3, 1, 6, 1, 6, 8, 1, 6, 7, 4 };
        int k = 5;
        Array.Sort(arr);
        int index = Array.BinarySearch(arr, k);
        if (index < 0)
        {
            index *= -1;
            for (int i = index; i >= 0; i--)
            {
                if (arr[i] < 5)
                {
                    Console.WriteLine(arr[i]);
                    break;
                }
            }
        }
        else
        {
            Console.WriteLine(arr[index]);
        }
    }
}